<div class="container">
<ul class="breadcrumb">
	<li><a href="<?php echo SITE_URL ?>/home/index">Home</a> <span class="divider">/</span></li>
	<li class="active">Post</li>
</ul>
<?php if(!empty($data)): ?>
<!-- pagination -->
<?php echo $paging; ?>
<!--/ pagination -->
<script language="javascript" type="text/javascript">
$(document).ready(function(){
	$('.delete-post').click(function(){
		var postId = $(this).attr('data-id');
		if(confirm('Sure?')){
			$.ajax({
				url:'<?php echo SITE_URL ?>/home/ajaxdeletepost/' + postId,
		     	error:function(jqXHR, textStatus, errorThrown){
		     		alert('Error when deleting post.');
		     	},
		     	success: function(data){
					if(data == 1){
						$('#row_' + postId).remove();
					}
					else{
						alert('Error when deleting post.');
					}
		     	}
			});
		}
	});
})
jQuery.fn.flash = function( fontSize, duration )
{
    var current = this.css('font-size');
    this.animate( { fontSize: fontSize }, duration / 2 );
    this.animate( { fontSize: current }, duration / 2 );
}
function closeDialog () {
	$('#windowTitleDialog').modal('hide'); 
};
function okClicked () {
	var postId = $(".modal-footer #modal-btn-update").attr('data-id');
	var post_title = $(".modal-body #post_title").val();
	var original_url = $(".modal-body #original_url").val();
	var post_image = $(".modal-body #post_image").val();
	var item_type = $(".modal-body #item_type").val();
	var source_page = $(".modal-body #source_page").val();
	var video_code = $(".modal-body #video_code").val();
	var category_id = $(".modal-body #category_id").val();
	$.ajax({
		url:'<?php echo SITE_URL ?>/home/ajaxupdatepost/' + postId,
     	dataType:'json',
     	error:function(jqXHR, textStatus, errorThrown){
     		alert('Error when updating post.');
     	},
     	type: "POST",
     	data:{'post_title':post_title, 'original_url':original_url, 'post_image':post_image, 'item_type':item_type, 'post_title':post_title, 'source_page':source_page, 'video_code':video_code, 'category_id':category_id},
     	success: function(data){
			closeDialog();
			$('#title_' + postId).flash( '25px', 1500 );
			$('#title_' + postId).html(post_title);
     	}
	});
	//closeDialog();
};
$(document).on("click", ".open-edit-dialog", function () {
     var postId = $(this).data('id');
     $.ajax({
     	url:'<?php echo SITE_URL ?>/home/ajaxpostinfo/' + postId,
     	dataType:'json',
     	error:function(jqXHR, textStatus, errorThrown){
     		alert('Error when getting post detail.');
     	},
     	success: function(data){
     		$(".modal-body #post_title").val(data.post_title);
     		$(".modal-body #original_url").val(data.original_url);
     		$(".modal-body #post_image").val(data.post_image);
     		$(".modal-body #item_type").val(data.item_type);
     		$(".modal-body #source_page").val(data.source_page);
     		$(".modal-body #video_code").val(data.video_code);
     		$(".modal-body #category_id").val(data.category_id);
     		$(".modal-footer #modal-btn-update").attr('data-id', postId);
     	}
     });
     
});
</script>
<div id="windowTitleDialog" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="windowTitleLabel" aria-hidden="true">
	<div class="modal-header">
		<a href="#" class="close" data-dismiss="modal">&times;</a>
		<h3>Update post</h3>
	</div>
	<div class="modal-body">
		<div class="divDialogElements">
		<form class="form-horizontal">
			<div class="control-group">
				<label class="control-label" for="inputEmail">Title</label>
				<div class="controls">
				<input type="text" id="post_title" class="input-xlarge">
				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="selectbasic">Category</label>
				  <div class="controls">
				    <select id="category_id" name="category_id" name="selectbasic" class="input-large">
				      <?php
				      $category = $this->config->item('categories');
				      if(!empty($category)){
				      	foreach($category as $id => $val){
				      		echo "<option value='$id'>$val[1]</option>\n";
				      	}
				      }
				      ?>
				    </select>
				  </div>
			</div>

			<div class="control-group">
				<label class="control-label" for="inputEmail">Original URL</label>
				<div class="controls">
				<input type="text" id="original_url" class="input-xlarge">
				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="inputEmail">Image</label>
				<div class="controls">
				<input type="text" id="post_image" class="input-xlarge">
				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="selectbasic">Type</label>
				  <div class="controls">
				    <select id="item_type" name="selectbasic" class="input-large">
				      <option value="photo">Photo</option>
				      <option value="video">Video</option>
				      <option value="gif">Gif</option>
				    </select>
				  </div>
			</div>

			<div class="control-group">
				<label class="control-label" for="inputEmail">Source</label>
				<div class="controls">
				<input type="text" id="source_page" class="input-xlarge">
				</div>
			</div>

			<div class="control-group">
				<label class="control-label" for="inputEmail">Video code</label>
				<div class="controls">
				<input type="text" id="video_code" class="input-xlarge">
				</div>
			</div>
		</form>
		</div>
	</div>
	<div class="modal-footer">
		<a href="#" class="btn" onclick="closeDialog();">Cancel</a>
		<a href="javascript:void(0)" id="modal-btn-update" class="btn btn-primary" onclick="okClicked();" data-id="">Update</a>
	</div>
</div>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<tr>
<th width="10"><input type="checkbox" name="checkall" value="<?php echo $post['id'] ?>"></th>
<th>Title</th>
<th width="150"></th>
<th width="40"> </th>
</tr>
<?php foreach($data as $post): ?>

<tr id="row_<?php echo $post['id']; ?>">
	<td><input type="checkbox" name="item[]" value="<?php echo $post['id'] ?>"></td>
	<td id="title_<?php echo $post['id']; ?>"><?php echo $post['post_title']; ?></td>
	<td><div style="max-height:200px; overflow:hidden;"><img src="<?php echo $img = strstr($post['post_image'], 'http') ? $post['post_image'] : IMAGE_URL.$post['post_image']; ?>" width="150" /></div></td>
	<td align="center">
	    <a data-backdrop="static" data-keyboard="false" class="open-edit-dialog" data-id="<?php echo $post['id']; ?>" data-toggle="modal" href="#windowTitleDialog" href="<?php echo SITE_URL.'/post/edit/'.$post['id']; ?>"><i class="icon-pencil"></i></a>
	    <a href="javascript:void(0)" class="delete-post" data-id="<?php echo $post['id'] ?>"><i class="icon-remove"></i></a>
	</td>
</tr>
<?php endforeach; ?>
</table>
<!-- pagination -->
<?php echo $paging; ?>
<!--/ pagination -->
</div>
<?php endif; ?>
</div>